<template>
  <div>
    <el-dialog
      :title="obj.isAdd == true ? '管理员添加' : '管理员编辑'"
      :visible.sync="obj.isTrue"
      @close="cancel()"
    ><el-form ref="form" :model="form">
      <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择所属角色">
            <el-option v-for="item in roleList" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button @click="submit()" v-if="obj.isAdd == true">添 加</el-button>
        <el-button type="primary" v-else @click="dits()">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { successAlert, warningAlert } from "../../../util/alter.js";
import { manageAdd,manageList,manageCount,manageDel,manageInfo,manageEdit } from "../../../requster/api.js";
import { mapGetters, mapActions } from "vuex";
export default {
  //接收父组件传来的参数
  props: ["obj"],
  data() {
    return {
      //菜单地址
      

      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      
    };
  },
  computed: {
    ...mapGetters({
      roleList: "role/list",
    }),
  },
  
  methods: {
    ...mapActions({
      reqroleList: "role/reqchangeList",
      reqmanagelist: "manage/reqchangelist",
    }),
    empty() {
      //清空封装
      this.form = {
        roleid: 1,
        username: "",
        password: "",
        status: 1,
      
      };
    },
    cancel() {
      //取消事件
      //改变父组件的传来的isTure
      this.obj.isTrue = false;

      this.empty();
    },
    submit() {
        console.log(this.form);
      //点击添加按钮
      manageAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqmanagelist();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条数据
    getone(uid) {
      manageInfo({ uid: uid }).then((res) => {
        
          this.form = res.data.list;
        
          
        
      });
    },
    dits() {
      manageEdit(this.form).then((res) => {
        console.log(this.form);
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqmanagelist();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted(){
    this.reqroleList()
  },
};
</script>

<style>
</style>